package ru.niir.goodfil.ui.utils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Vit
 *         Добавляется информация в хедеры, что бы не сохранялся кеш.
 */
public class NoCacheFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        try {
            if (resp instanceof HttpServletResponse) {
                HttpServletResponse httpresponse = (HttpServletResponse) resp;
                httpresponse.setHeader("Cache-Control", "no-cache");
                httpresponse.setHeader("Expires", "0");
                String name = ((HttpServletRequest) req).getRequestURI();
            }
            chain.doFilter(req, resp);
        } catch (IOException e) {
            System.out.println("IOException in NoCacheFilter");
            e.printStackTrace();
        } catch (ServletException e) {
            System.out.println("ServletException in NoCacheFilter");
            e.printStackTrace();
        }
    }


    public void init(FilterConfig config) throws ServletException {

    }

}
